<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserContext;
use App\Http\Requests\UserLogin;
use App\Http\Requests\UserRegister;
use App\Http\Services\UserService;
use App\Http\Traits\ResponseJson;
use GenTux\Jwt\JwtToken;

class UserController extends Controller
{
    use ResponseJson;

    protected $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    /**
     * 账号密码登录
     * @param UserLogin $userLogin
     * @param JwtToken $jwtToken
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ServiceException
     */
    public function loginPass(UserLogin $userLogin, JwtToken $jwtToken)
    {
        $data = $this->userService->loginPass($userLogin,$jwtToken);
        return $this->returnJson($data);
    }

    /**
     * 注册
     * @param UserRegister $userRegister
     * @param JwtToken $jwtToken
     * @return \Illuminate\Http\JsonResponse
     */
    public function register(UserRegister $userRegister,JwtToken $jwtToken)
    {
        $data = $this->userService->registerUser($userRegister,$jwtToken);
        return $this->returnJson($data);
    }


    /**
     * 退出登录
     * @param UserContext $userContext
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout(UserContext $userContext)
    {
        $data = $this->userService->logout($userContext);
        return $this->returnJson($data);
    }

}
